package com.zust.ysc012.servlet;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import static com.zust.ysc012.dao.TeacherDao.insert_project_sql;
import static com.zust.ysc012.dao.TeacherDao.select_project_id_sql;

/**
 * @Description
 * @Author Mato
 * @Date 04/07/2021 02:37
 */
@WebServlet(urlPatterns = "/uploadServlet")
public class UploadServlet extends HttpServlet {
    @Override
    public void init() throws ServletException {
        super.init();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        HttpSession session = req.getSession(true);
        String account = (String) session.getAttribute("account");
        String name = null;
        String startTime;
        String deadline;
        Date date1 = null;
        Date date2 = null;
        int i;
        boolean multipartContent = ServletFileUpload.isMultipartContent(req);
        if (multipartContent) {
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            try {
                List<FileItem> items = upload.parseRequest(req);
                Iterator<FileItem> iter = items.iterator();
                while (iter.hasNext()) {
                    FileItem item = iter.next();
                    if (item.isFormField()) {
                        if (item.getFieldName().equals("name")) {
                            name = item.getString("UTF-8");
                            System.out.println(name);
                        } else if (item.getFieldName().equals("startTime")) {
                            startTime = item.getString("UTF-8");
                            startTime = startTime.substring(6,10) + "-" + startTime.substring(0,2) + "-" + startTime.substring(3,5) + " " + startTime.substring(11,16);
                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                            date1 = simpleDateFormat.parse(startTime);
                        } else if (item.getFieldName().equals("deadline")) {
                            deadline = item.getString("UTF-8");
                            deadline = deadline.substring(6,10) + "-" + deadline.substring(0,2) + "-" + deadline.substring(3,5) + " " + deadline.substring(11,16);
                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                            date2 = simpleDateFormat.parse(deadline);
                        }
                    } else {
                        System.out.println("what");
                        String fileName = item.getName();
                        String ext = fileName.substring(fileName.indexOf(".") + 1);
                        String path = "C:\\C\\design\\src\\main\\webapp\\WEB-INF\\upload";
                        i = select_project_id_sql() + 1;
                        fileName = i + "." + ext;
                        File file = new File(path, fileName);
                        item.write(file);
                    }
                }
                insert_project_sql(account, name, date1, date2);
                req.getRequestDispatcher("/WEB-INF/teacher/uploaded.jsp").forward(req,resp);
            } catch (FileUploadBase.SizeLimitExceededException e) {
                e.printStackTrace();
            } catch (FileUploadException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
